package com.dodayum.service;

import com.dodayum.entity.DemoEntity;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DemoEntityService {
    @Resource
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    public List<DemoEntity> query(){
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withPageable(PageRequest.of(0, 100));
        NativeSearchQuery build = nativeSearchQueryBuilder.build();
        SearchHits<DemoEntity> search = elasticsearchRestTemplate.search(build, DemoEntity.class);
        List<DemoEntity> response = new ArrayList<>();
        for (SearchHit<DemoEntity> searchHit : search.getSearchHits()) {
            DemoEntity content = searchHit.getContent();
            response.add(content);
        }
        return response;
    }
}
